
import { baseUrl } from './base_url'

/**
 * 发起请求
 * @param {Object} config - 参数
 * @returns {Promise}
 */
const startRequest = (config) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + config.url,
      data: config.params,
      method: config.method,
      header: {
        'Content-Type': config.contentType
      },
      success: (response) => {
        // console.log("请求结果", response.data)
        if (response.data.code === 3) {
          uni.showToast({
            title: response.data.message,
            icon: 'none'
          });
          uni.clearStorageSync();
        }
        resolve(response.data);
      },
      fail: (err) => {
        console.log("错误");
        reject(err);
        uni.showModal({
          title: '提示',
          content: '请重新连接到网络~',
        });
      },
      complete: () => {
        uni.hideLoading();
      }
    });
  });
};

const startUpload = (config) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: baseUrl + config.url,
      filePath: config.file,
      name: config.name,
      header: {
        'Content-Type': "application/x-www-form-urlencoded"
      },
      onUploadProgress: (progressEvent) => {
        console.log(progressEvent);
        var percentCompleted = Math.round(
          (progressEvent.loaded * 100) / progressEvent.total
        );
        uni.showLoading({
          title: '文件上传中'
        });
      },
      success: (response) => {
        resolve(JSON.parse(response.data));
      },
      fail: (err) => {
        console.log("错误");
        reject(err);
        uni.showModal({
          title: '提示',
          content: '网络错误!',
        });
      },
      complete: () => {
        uni.hideLoading();
      }
    });
  });
};

/**
 * 封装请求参数
 * @param {string} url - 地址
 * @param {Object} params - 负载
 * @param {string} method - 请求方式
 * @param {string} contentType - 内容类型
 * @returns {Object}
 */
const packageParam = (url, params, method, contentType) => {
  return {
    url: url,
    params: params,
    method: method,
    contentType: contentType
  };
};

const packageParamUpload = (url, file, name) => {
  return {
    url: url,
    name: name,
    file: file
  };
};

export function request_post(url, params) {
  let param = packageParam(url, params, 'POST', "application/x-www-form-urlencoded");
  return startRequest(param);
}

export function request_get(url, params) {
  let param = packageParam(url, params, "GET", "application/x-www-form-urlencoded");
  return startRequest(param);
}

export function request_json_post(url, params) {
	console.log(params);
  let param = packageParam(url, params, "POST", "application/json");
  return startRequest(param);
}

export function request_json_get(url, params) {
  let param = packageParam(url, params,	"GET", "application/json");
  return startRequest(param);
}

export const uploadFile = (url, file, name) => {
  let param = packageParamUpload(url, file, name);
  return startUpload(param);
}
